import type {Directive} from "vue";
import {useUserStore} from "@/store/user";

export const permission: Directive = {
    //自定义按钮权限指令
    mounted(el, binding) {
        const store = useUserStore()
        const {value} = binding
        //当前用户的所有权限
        const permissions = store.getCodeList
        if (value && value instanceof Array && value.length > 0) {
            const permissionRoles = value
            //判断按钮权限是否存在于用户权限列表
            const hasPermission = permissions.some((role) => {
                return permissionRoles.includes(role)
            })
            if (!hasPermission) {
                //没有权限隐藏按钮
                el.style.display = 'none'
            }
        } else {
            throw new Error('按钮权限的传递方式v-permission=["sys:role:add"]')
        }
    }
}